﻿#include <Arduino.h>
#include <hal_pir.h>
#include <global_config.h>

HalPIR halPIR;
int calibrationTime = 30;
long unsigned int lowIn;
long unsigned int pauseTime = 5000;
boolean lockLow = true;
boolean takeLowTime;
int PIRValue = 0;

void HalPIR::init()
{
    Serial.println("Init PIR sensor...");
    pinMode(PIR_PIN, INPUT);
}


void HalPIR::update()
{
    if (digitalRead(PIR_PIN) == HIGH) {
    if (lockLow) {
        PIRValue = 1;
        lockLow = false;
        Serial.println("Motion detected.");
        delay(50);
    }
    takeLowTime = true;
    }
    if (digitalRead(PIR_PIN) == LOW) {
    if (takeLowTime) {
        lowIn = millis(); takeLowTime = false;
    }
    if (!lockLow && millis() - lowIn > pauseTime) {
        PIRValue = 0;
        lockLow = true;
        Serial.println("Motion ended.");
        delay(50);
    }
    }
}
